#include <..\\standard.hlsli>
#include <..\\WR2Parameters.hlsli>

VertexToPixel vs_main(VS_INPUT Input)
{
	VertexToPixel Output = (VertexToPixel)0;
	Output.pos = float4(mul(Input.Position, worldViewProj));
	Output.normal = mul(Input.Normal, (float3x3)world);

	Output.kColor = float4(Input.kColor.rgb * 2, Input.kColor.a);// red green blue shadow
	Output.kBlend = float4(Input.kblend);

	Output.pos2 = Input.Position;

	Output.texCoord1 = float4(Input.Tex.x, Input.Tex.y, 0, 0);

	float d = distance(Input.Position, 0);
	float fFogStart = 2000;
	float fFogEnd = 2500;
	Output.foger = saturate((fFogEnd - d) / (fFogEnd - fFogStart));
	return Output;
};

struct PixelToFrame
{
	float4 Color;
};

PixelToFrame ps_main(VertexToPixel PSIn) : SV_TARGET
{
	PixelToFrame Output = (PixelToFrame)0;

	float3 Lp = -lightDir;
		float3 Nn = normalize(PSIn.normal);
		float3 In = normalize(PSIn.pos2.xyz - CameraPosition);// View direction
		float3 Hn = normalize(Lp - In);         // Half way between view & light

		float4 Lay1 = xColoredTexture1_Tex.Sample(Tex1, PSIn.texCoord1.xy);

		float ShadowCol = smoothstep(0.4375, 0.5625, 1.0 - PSIn.kBlend.a);

	float diffuseColor = max(0.0f, dot(Nn, Lp)) * 1.8f;
	float spec = pow(max(0.0f, dot(Hn, Nn)), 16) * 0.5;
	float3 TexColor = Lay1.rgb*(1.0 + spec);
		Output.Color = float4(TexColor *(SunColor* diffuseColor * ShadowCol + PSIn.kColor.rgb), 1);
	Output.Color = float4(lerp(Output.Color.rgb, FogColor, PSIn.texCoord3.z), Output.Color.a);
	return  Output;
}

technique Material32
{
	pass Material00
	{
		VertexShader = compile vs_1_1 vs_main();
		PixelShader = compile ps_2_0 ps_main();
	}
}